package com.wd.tech.utils;


import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author 时文豪
 * @description:  网络工具类
 * @date :2020/7/2 11:01
 */
public class NetUtils {
    private String url="https://mobile.bwstudent.com/";
    private Apis apis;

    private NetUtils(){
        initModel();
    }
    private static class MyNetUtils{
        private static final NetUtils NET_UTILS=new NetUtils();
    }
    public static NetUtils getInstance(){
        return MyNetUtils.NET_UTILS;
    }
    private void initModel() {
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.readTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10,TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                //调用登录过后的UserId和SeissanId
               // .addInterceptor(new IntreOfCet())
                .addInterceptor(httpLoggingInterceptor);
        OkHttpClient build = builder.build();
        Retrofit build1 = new Retrofit.Builder().baseUrl(url).client(build)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        apis = build1.create(Apis.class);
    }

    public Apis getApis() {
        return apis;
    }
//获取UserId跟SessionId进行相关联
 /*  private class IntreOfCet implements Interceptor {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            //通过Sp进行获取UserId跟sessionId
            //实现代码就不写了让登录模块来写


            //取出之后进行判断   通过TextUtils.isEmpty  判断userId跟sessionId
            *//***
             *  例子： if(TextUtils.isEmpty(userId)||TextUtils.isEmpty(sessionId)){
             *                 return chain.proceed(request);
             *             }
             *//*

            //返回值是 chain.proceed(request);
          //  Request build = request.newBuilder().addHeader("userId", userId).addHeader("sessionId", sessionId).build();
            return null;//return 返回的是    chain.proceed(request);
        }
    }*/
    //图片上传
    public RequestBody getRequestBody(List<File> files, HashMap<String,String>map){
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        for (Map.Entry<String,String>entry:map.entrySet()){
          builder.addFormDataPart(entry.getKey(),entry.getValue());
        }
        for (int i = 0; i < files.size(); i++) {
            builder.addFormDataPart("image",files.get(i).getName(),RequestBody.create(MediaType.parse("image/jepg"),files.get(i)));
        }
        return builder.build();
    }
}
